package com.situ.crm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Customer;
import com.situ.crm.entity.Order;
import com.situ.crm.entity.Product;
import com.situ.crm.entity.SaleChance;
import com.situ.crm.entity.User;
import com.situ.crm.service.ICustomerService;
import com.situ.crm.service.IOrderService;
import com.situ.crm.service.IProductService;
import com.situ.crm.service.ISaleChanceService;
import com.situ.crm.service.IUserService;
import com.situ.crm.util.UserContext;

@Controller
@RequestMapping(value="/saleChance")
public class SaleChanceController {
	@Autowired
	private ISaleChanceService saleChanceService;
	@Autowired
	private IOrderService orderService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IProductService productService;
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="/getSaleChancePage")
	public String getSaleChancePage() {
		return "salechance/salechance_list";
	}
	
	//分页
	@RequestMapping(value="/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page,Integer limit,SaleChance saleChance) {
		ServerResponse serverResponse = saleChanceService.pageList(page,limit,saleChance);
		System.err.println(saleChance);
		return serverResponse;
	}
	
	//添加页面
	@RequestMapping(value="/getAddPage")
	public String getAddPage(Model model) {
		//添加页面选择客户、意向产品、营销人员
		List<Customer> customers = customerService.selectAll();
		List<Product> product = productService.selectAll();
		List<User> user = userService.selectXiaoshouUser();
		model.addAttribute("customers",customers);
		model.addAttribute("product",product);
		model.addAttribute("user",user);
		return "salechance/salechance_insert";
	}
	
	//添加
	@RequestMapping(value="/insert")
	@ResponseBody
	public ServerResponse insert(SaleChance saleChance,HttpSession session) {
		System.err.println(saleChance);
		User user = (User) session.getAttribute(UserContext.USER_IN_SESSION);
		saleChance.setCreateMan(user.getName());
		return saleChanceService.insert(saleChance);
	}
	
	//根据id删除
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		return saleChanceService.deleteById(id);
	}
	
	//批量删除
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public ServerResponse deleteById(String ids) {
		return saleChanceService.deleteAll(ids);
	}
	
	//修改查询
	@RequestMapping(value="/getUpdatePage")
	public String saleChance(Integer id,Model model) {
		SaleChance saleChance = saleChanceService.select(id);
		model.addAttribute("saleChance",saleChance);
		
		List<Customer> customers = customerService.selectAll();
		List<Product> product = productService.selectAll();
		List<User> user = userService.selectXiaoshouUser();
		model.addAttribute("customers",customers);
		model.addAttribute("product",product);
		model.addAttribute("user",user);
		return "salechance/salechance_update";
	}
	
	//修改
	@RequestMapping(value="/update")
	@ResponseBody
	public ServerResponse update(SaleChance saleChance) {
		System.err.println(saleChance);
		return saleChanceService.update(saleChance);
	}
	
	//点击开发成功，填充order表，根据sale_chance的id查询值
	@RequestMapping(value="/selectById")
	@ResponseBody
	public ServerResponse selectById(Integer id){
		//查出sale_chance表的信息
		SaleChance saleChance = saleChanceService.selectById(id);
		System.err.println(saleChance);
		
		//填充order表
		Order order = new Order();
		order.setCustomerId(saleChance.getCustomerId());//客户ID
		//生成时间的订单号
		Date date = new Date();
		SimpleDateFormat toOrderNo = new SimpleDateFormat("yyyyMMddhhmmss");
		String orderNo = toOrderNo.format(date);
		order.setOrderNo(orderNo);//订单号
		order.setSaleChanceId(saleChance.getId());//机会id
		//订购日期
		order.setOrderDate(date);//订购日期
		order.setStatus(1);//回款
		order.setProductId(saleChance.getProductId());//产品id
		
		//更改sale_chance表的客户状态,更改为开发成功
		saleChance.setDevResult(2);
		saleChanceService.update(saleChance);
		
		return orderService.insert(order);
	}
	
	//开发失败,更改sale_chance的客户开发状态
	@RequestMapping(value="/loser")
	@ResponseBody
	public ServerResponse loser(Integer id) {
		//查出sale_chance表的信息
		SaleChance saleChance = saleChanceService.selectById(id);
		saleChance.setDevResult(3);
		return saleChanceService.update(saleChance);
	}
	
}
